package com.burgeon.backend.SHOPBACKEND.rest.evaluate;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.evaluate.EvaluateVO;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;


public class FindEvaluate extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public FindEvaluate(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    /**
     * 接收REP返回的字符串并进行数据处理
     *
     * @return
     */
    public ArrayList<EvaluateVO> getData(String field,String message, int start) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(field,message,start), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<EvaluateVO> evaluateVOS = new ArrayList<EvaluateVO>();
        for (int i = 0; i < jsonArray.length(); i++) {
            EvaluateVO evaluateVO = new EvaluateVO();
            JSONArray array = jsonArray.getJSONArray(i);
            evaluateVO.setCount(count);
            evaluateVO.setId(array.getString(0));
            evaluateVO.setEvaluate(array.getString(1));
            evaluateVO.setGrade(array.getString(2));
            evaluateVO.setVipPhone(array.getString(3));
            evaluateVO.setVipName(array.getString(4));
            evaluateVO.setYcOrderId(array.getString(5));
            evaluateVO.setGoodsCode(array.getString(6));
            evaluateVO.setIsShow(array.getString(7));
            evaluateVO.setCreateTime(array.getString(8));
            evaluateVO.setStore(array.getString(9));
            evaluateVO.setReply(array.getString(10));
            evaluateVOS.add(evaluateVO);

        }

        return evaluateVOS;
    }

    /**
     * 准备向ERP传送字段拼接
     *
     * @return
     */
    private MultiValueMap Data(String field,String message,int y) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");//ID
        columns.add("DESCRIPTION");//评价
        columns.add("AVERAGE");// 	分数
        columns.add("'C_VIP_ID;MOBIL'");//vipPhone
        columns.add("'C_VIP_ID;VIPNAME'");//vipName
        columns.add("'YC_RETAIL_ID;DOCNO'");// 云仓单号
        columns.add("'M_PRODUCT_ID;NAME'");//goodsCode
        columns.add("IS_SHOW");//显示隐藏 YN
        columns.add("CREATIONDATE");// createTime
        columns.add("'C_STORE_ID;NAME'");//store
        columns.add("ANSWER");//回复


        params.put("columns", columns);
        params.put("table", "YC_VALUATION");//表名

        //设置查询规则（从第几条查询，每页显示多少条数）
        params.put("range", RANGE);
        params.put("count", "true");
        int r = Integer.valueOf(RANGE);
        params.put("start", String.valueOf(y * r));

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        //*************************
        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();

        expr1.put("column", "'"+field+"'");
        expr1.put("condition", message);

        HashMap expr2 = new HashMap();
        String is = "'=Y'";
        expr2.put("column","ISACTIVE");
        expr2.put("condition",is);

        paramss.put("combine", "and");
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        params.put("params", paramss);


        return DATA("Query", params, account, password);

    }

    public static void main(String[] args) {
        FindEvaluate querryYCRetailout = new FindEvaluate("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(querryYCRetailout.getData("","",0));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
